import {
    RequestManagerList,
    RequestRoleList,
    RequestRegister,
    RequestManagerDelete,
    RequestMangerUpdate,
} from '@/api/path/manager'
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import Sha256 from 'js-sha256'
import { api } from '@/api/index'
const { RequestRoleDelete } = api
/**
 * 账户业务
 */
export const useManager = () => {
    const managerList = ref([])
    // 账户列表
    const getManagerList = () => {
        RequestManagerList()
            .then(data => {
                let { resultCode, resultInfo } = data
                if (resultCode == 1) {
                    managerList.value = resultInfo
                }
            })
            .catch(error => {
                ElMessage.error('获取数据出错' + error)
            })
    }
    onMounted(() => {
        getManagerList()
    })
    //过滤url地址
    const onFilterUrl = url => {
        if (url) {
            if (url.indexOf('http') !== -1) {
                return url
            } else {
                let baseURL = 'http://localhost:8089'
                return baseURL + url
            }
        }
    }
    //删除账户
    const onDelete = id => {
        RequestManagerDelete(id).then(data => {
            let { resultCode } = data
            if (resultCode == 1) {
                ElMessage.success('删除成功!')
                getManagerList()
            }
        })
    }

    const onClose = () => {
        getManagerList()
    }

    return { managerList, onFilterUrl, onDelete, onClose }
}
/**
 * 角色业务
 * @returns
 */
export const useRole = () => {
    const displayAuthorDialog = ref(false)
    let displayRoleDialog = ref(false)
    const roleList = ref([])
    let roleId = ref(0)
    let displayAuthorResDialog = ref(false)

    onMounted(() => {
        getRoleList()
    })
    const getRoleList = () => {
        RequestRoleList()
            .then(data => {
                if (!data) return
                let { resultCode, resultInfo } = data
                if (resultCode == 1) {
                    roleList.value = resultInfo.roles
                }
            })
            .catch(error => {
                console.error(error)
            })
    }
    const onAuthorManager = role_Id => {
        roleId.value = role_Id
        displayAuthorDialog.value = true
    }
    const onAuthorResource = role_Id => {
        roleId.value = role_Id
        displayAuthorResDialog.value = true
    }
    const onAddRole = () => {
        displayRoleDialog.value = true
    }

    const onRoleDialogClose = () => {
        displayRoleDialog.value = false
        getRoleList()
    }

    const onDelete = (id:number) => {
        RequestRoleDelete(id).then(data => {
            if (data.resultCode === 1) {
                getRoleList()
                ElMessage.success('删除成功')
            }
        })
    }
	//编辑
	const onEdit = ()=>{
		ElMessage.success('开发中!')
	}
    return {
        roleId,
        roleList,
        displayAuthorDialog,
        displayRoleDialog,
        displayAuthorResDialog,
        onAuthorManager,
        onAddRole,
        onRoleDialogClose,
        onDelete,
        onAuthorResource,
		onEdit
    }
}

/**
 * 添加账户
 */
export const useManagerAdd = emit => {
    const user = ref({
        username: '',
        password: '',
        headerimg: '',
        nick: '',
        role_id: '',
    })
    let imageUrl = ref('') //预览图片
    const registerForm = ref() // ref表单节点
    const rules = {
        username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    }
    const onChangeFile = file => {
        user.value.headerimg = file.raw
        imageUrl.value = URL.createObjectURL(file.raw)
    }
    // 上传之前执行，一般验证图片大小和格式
    const beforeAvatarUpload = rawFile => {
        let picArr = ['image/jpeg', 'image/png']
        if (picArr.indexOf(rawFile.type) == -1) {
            ElMessage.error('上传图片必须是jpeg或png图片')
            return false // 终止上传
        } else if (rawFile.size / 1024 / 1024 > 2) {
            ElMessage.error('图片大小超过 2MB!')
            return false
        }
        return true
    }
    const closeDialog = () => {
        emit('update:modelValue', false)
    }
    const onSubmit = registerForm => {
        registerForm.validate(valid => {
            if (valid) {
                if (user.value.m_id) {
                    // ElMessage.success('编辑接口开发中')
                    RequestMangerUpdate(user.value).then(data => {
                        if (data.resultCode == 1) {
                            ElMessage.success('编辑成功!')
                            emit('close')
                            closeDialog()
                        }
                    })
                } else {
                    let formData = new FormData()
                    formData.append('username', user.value.username)
                    formData.append('password', Sha256(user.value.password))
                    formData.append('nick', user.value.nick)
                    formData.append('roseid', user.value.role_id)
                    formData.append('headerimg', user.value.headerimg)
                    RequestRegister(formData).then(data => {
                        let { resultCode, resultInfo } = data
                        if (resultCode === 1) {
                            ElMessage({
                                type: 'success',
                                message: '注册成功!',
                            })
                            // emit('update:modelValue', false)
                            emit('close')
                            closeDialog()
                        } else {
                            ElMessage({
                                type: 'error',
                                message: '注册失败!' + resultInfo,
                            })
                        }
                    })
                }
            }
        })
    }

    return {
        user,
        rules,
        imageUrl,
        registerForm,
        onChangeFile,
        beforeAvatarUpload,
        onSubmit,
    }
}

/**
 * managerDialog
 */
export const useManagerDialog = () => {
    const displayManagerDialog = ref(false)
    const title = ref('')
    let user = ref({})

    //添加账户
    const addManager = () => {
        title.value = '添加账户'
        displayManagerDialog.value = true
        user.value = {
            username: '',
            password: '',
            headerimg: '',
            nick: '',
            role_id: '',
        }
    }
    //删除账户
    const onEdit = userEdit => {
        title.value = '编辑账户'
        displayManagerDialog.value = true
        user.value = userEdit
    }

    return { user, displayManagerDialog, title, addManager, onEdit }
}
